package com.museum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.museum.common.Result;
import com.museum.entity.Review;
import com.museum.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 评价控制器
 */
@RestController
@RequestMapping("/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 提交评价
     * @param review 评价信息
     * @return 提交结果
     */
    @PostMapping("/submit")
    public Result<Boolean> submitReview(@RequestBody Review review) {
        boolean success = reviewService.addReview(review);
        if (!success) {
            return Result.error("提交评价失败，可能已经评价过了");
        }
        return Result.success(true);
    }

    /**
     * 管理员回复评价
     * @param reviewId 评价ID
     * @param adminId 管理员ID
     * @param reply 回复内容
     * @return 回复结果
     */
    @PostMapping("/reply")
    public Result<Boolean> replyReview(
            @RequestParam("reviewId") Long reviewId,
            @RequestParam("adminId") Long adminId,
            @RequestParam("reply") String reply) {
        boolean success = reviewService.replyReview(reviewId, adminId, reply);
        if (!success) {
            return Result.error("回复失败");
        }
        return Result.success(true);
    }

    /**
     * 获取用户的评价列表
     * @param userId 用户ID
     * @return 评价列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<Map<String, Object>>> getUserReviews(@PathVariable Long userId) {
        List<Map<String, Object>> reviews = reviewService.getUserReviews(userId);
        return Result.success(reviews);
    }

    /**
     * 获取预约的评价
     * @param reservationId 预约ID
     * @return 评价信息
     */
    @GetMapping("/reservation/{reservationId}")
    public Result<Map<String, Object>> getReservationReview(@PathVariable Long reservationId) {
        Map<String, Object> review = reviewService.getReservationReview(reservationId);
        if (review == null) {
            return Result.error(404, "该预约暂无评价");
        }
        return Result.success(review);
    }

    /**
     * 分页获取全部评价（管理端使用）
     * @param page 页码
     * @param size 每页大小
     * @param hasReply 是否已回复
     * @return 分页评价列表
     */
    @GetMapping("/admin/list")
    public Result<Page<Map<String, Object>>> pageReviews(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "hasReply", required = false) Boolean hasReply) {
        Page<Review> pageParam = new Page<>(page, size);
        Page<Map<String, Object>> result = reviewService.pageReviews(pageParam, hasReply);
        return Result.success(result);
    }
} 